<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\LogisticsCompany;
use Illuminate\Http\Request;
use Inertia\Inertia;

class LogisticsCompanyController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $companies = LogisticsCompany::ordered()->paginate(20);
        
        return redirect()->route('admin.system-settings', ['group' => 'logistics'])
            ->with('companies', $companies);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return redirect()->route('admin.system-settings', ['group' => 'logistics']);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'code' => 'required|string|max:50|unique:logistics_companies,code',
            'website' => 'nullable|url|max:255',
            'phone' => 'nullable|string|max:50',
            'description' => 'nullable|string|max:1000',
            'is_active' => 'boolean',
            'sort_order' => 'integer|min:0'
        ]);

        LogisticsCompany::create($request->all());

        return redirect()->route('admin.system-settings', ['group' => 'logistics'])
            ->with('success', '物流公司创建成功');
    }

    /**
     * Display the specified resource.
     */
    public function show(LogisticsCompany $logisticsCompany)
    {
        return redirect()->route('admin.system-settings', ['group' => 'logistics']);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(LogisticsCompany $logisticsCompany)
    {
        return redirect()->route('admin.system-settings', ['group' => 'logistics']);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, LogisticsCompany $logisticsCompany)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'code' => 'required|string|max:50|unique:logistics_companies,code,' . $logisticsCompany->id,
            'website' => 'nullable|url|max:255',
            'phone' => 'nullable|string|max:50',
            'description' => 'nullable|string|max:1000',
            'is_active' => 'boolean',
            'sort_order' => 'integer|min:0'
        ]);

        $logisticsCompany->update($request->all());

        return redirect()->route('admin.system-settings', ['group' => 'logistics'])
            ->with('success', '物流公司更新成功');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(LogisticsCompany $logisticsCompany)
    {
        $logisticsCompany->delete();

        return redirect()->route('admin.system-settings', ['group' => 'logistics'])
            ->with('success', '物流公司删除成功');
    }

    /**
     * 获取启用的物流公司列表（API）
     */
    public function getActiveCompanies()
    {
        $companies = LogisticsCompany::active()->ordered()->get(['id', 'name', 'code']);
        
        return response()->json($companies);
    }
}
